import { View } from "../../../../mvc/core/View";
import { Cache } from "../../../cache/Cache";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../../common/alertwins/CheckBoxWin";
import { Language } from "../../../../../extend/language/Language";
import { WuXingDiceWordItem } from "./WuXingDiceWordItem";
import { SWuXingDice } from "../../../../../Message/Public/SWuXingDice";
import { WuXingConfig } from "../../../resource/WuXingConfig";
import { GameConst } from "../../../../component/gconst/GameConst";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { EffectManager } from "../../../manager/EffectManager";
type int = number;
//class WuxingDiceResultView
    
    export  class WuxingDiceResultView extends View
    {
        constructor()
        {
            super();
            this._wordItemArray = [];
            this.init();
            return;
        }

        public get isMax(): Boolean
        {
            this._isMax = this.getMainAttributeNum(this._dices) == 6;
            return this._isMax;
        }

        /* internal  */getSortDices(arg1: Array<any>): Array<any>
        {
            var loc1=null;
            var loc5=0;
            var loc2=[];
            var loc3=[];
            var loc4=Cache.instance.role.wuxingInfo.mainAttribute;
            var loc6=0;
            while (loc6 < arg1.length) 
            {
                loc5 = arg1[loc6] as int;
                if (loc4 != loc5) 
                {
                    loc3.push(loc5);
                }
                else 
                {
                    loc2.push(loc5);
                }
                ++loc6;
            }
            loc1 = loc2.concat(loc3);
            return loc1;
        }

        /* internal  */getMainAttributeNum(arg1: Array<any>): int
        {
            var loc1=0;
            var loc3=0;
            var loc2=Cache.instance.role.wuxingInfo.mainAttribute;
            var loc4=0;
            while (loc4 < arg1.length) 
            {
                loc3 = arg1[loc4] as int;
                if (loc2 == loc3) 
                {
                    ++loc1;
                }
                ++loc4;
            }
            return loc1;
        }

        /* internal  */onBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            var e: Object /* flash.events.Event */;
            var name: string;
            var isNeedAlert: Boolean;
            var onBtnClickHandler: Function;

            var loc1;
            isNeedAlert = false;
            e = arg1;
            name = (e.target as GButton).name;
            if (name != "get") 
            {
                if (this._isMax) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.WuXingThrowDiceReq, {"type": 2, "isActive": false}));
                    return;
                }
                if (this._freeLeftTime == 0 && this._cost > 0) 
                {
                    isNeedAlert = !PulseSharedObject.isTodayNotTips("WuXingDiceAlert");
                    if (isNeedAlert) 
                    {
                        onBtnClickHandler = (arg1: int, arg2: Boolean): void
                        {
                            if (arg1 == Alert.OK) 
                            {
                                Dispatcher.dispatchEvent(new DataEvent(EventName.WuXingThrowDiceReq, {"type": 1, "isActive": false}));
                                Alert.resetToDefault();
                            }
                            if (arg2) 
                            {
                                PulseSharedObject.save("WuXingDiceAlert", true);
                            }
                            return;
                        }
                        Alert.alertWinRenderer = CheckBoxWin;
                        Alert.show(Language.getStringByParam(26210, this._cost), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                    }
                    else 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.WuXingThrowDiceReq, {"type": 1, "isActive": false}));
                    }
                }
                else 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.WuXingThrowDiceReq, {"type": 1, "isActive": false}));
                }
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.WuXingThrowDiceReq, {"type": 2, "isActive": false}));
            }
            return;
        }

        /* internal  */updateWordItem(arg1: int, arg2: int, arg3: Array<any>): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._wordItemArray.length) 
            {
                (loc1 = this._wordItemArray[loc2] as WuXingDiceWordItem).freeLeftTimes = arg1;
                loc1.cost = arg2;
                loc1.wuxing = arg3[loc2] as int;
                ++loc2;
            }
            return;
        }

        public updateBuWuXingDice(arg1: SWuXingDice): void
        {
            this._wxDice = arg1;
            if (arg1) 
            {
                this._mainAttr = Cache.instance.role.wuxingInfo.mainAttribute;
                this._mainAttrName = Cache.instance.role.wuxingInfo.mainAttrName;
                this._dices = this.getSortDices(arg1.dice);
                this._isMax = this.isMax;
                this._sameNum = this.getMainAttributeNum(this._dices);
                this._addEnergy = WuXingConfig.instance.getWuxingEnergy(this._sameNum);
                this._freeLeftTime = GameConst.WuxingFreeChangeCount - arg1.toModifyCount;
                this._freeLeftTime = this._freeLeftTime > 0 ? this._freeLeftTime : 0;
                this._cost = WuXingConfig.instance.getChangeEnergyCost(this._wxDice.diceMoneyModifyCount + 1);
                if (this._freeLeftTime > 0) 
                {
                    this._retryBtn.label = Language.getString(26209);
                    this._retryBtn.toolTipData = Language.getStringByParam(26215, this._freeLeftTime);
                    this.showTxtEffect(true, this._retryBtn, 0);
                    this._labelTxt.text = Language.getString(26212);
                    this._freeLeftTimeTxt.text = this._freeLeftTime.toString();
                }
                else 
                {
                    this._retryBtn.label = Language.getString(26214);
                    this._retryBtn.toolTipData = Language.getStringByParam(26213, this._cost);
                    this.showTxtEffect(false, this._retryBtn);
                    this._labelTxt.text = Language.getString(26225);
                    this._freeLeftTimeTxt.text = this._cost.toString();
                }
                if (this._isMax) 
                {
                    this.showTxtEffect(false, this._retryBtn);
                    this._retryBtn.label = Language.getString(26208);
                    this._retryBtn.toolTipData = null;
                }
                if (this._sameNum < 4) 
                {
                    this._nameTxt.htmlText = this._sameNum + this._mainAttrName;
                }
                else if (this._sameNum != 4) 
                {
                    if (this._sameNum != 5) 
                    {
                        if (this._sameNum == 6) 
                        {
                            this._nameTxt.htmlText = Language.getStringByParam(26219, this._mainAttrName);
                        }
                    }
                    else 
                    {
                        this._nameTxt.htmlText = Language.getStringByParam(26218, this._mainAttrName);
                    }
                }
                else 
                {
                    this._nameTxt.htmlText = Language.getStringByParam(26217, this._mainAttrName);
                }
                if (this._sameNum == this._preSameNum) 
                {
                    this.showTxtEffect(false, this._nameTxt);
                }
                else 
                {
                    this.showTxtEffect(true, this._nameTxt);
                }
                this._preSameNum = this._sameNum;
                this._valueTxt.text = Language.getStringByParam(26207, this._addEnergy);
                if (this._addEnergy == this._preAddEnergy) 
                {
                    this.showTxtEffect(false, this._valueTxt);
                }
                else 
                {
                    this.showTxtEffect(true, this._valueTxt);
                }
                this._preAddEnergy = this._addEnergy;
            }
            return;
        }

        /* internal  */init(): void
        {
            this._getBtn = ObjCreate.createGButton(Language.getString(26208), 79, 0, 60, 24, this, "ButtonRed");
            this._getBtn.name = "get";
            this._retryBtn = ObjCreate.createGButton(Language.getString(26209), 149, 0, 60, 24, this, "ButtonRed");
            this._getBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._retryBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._labelTxt = ObjCreate.createTextField(Language.getString(26212), 212, 2, 200, 20, this, GlobalStyle.textFormat6);
            this._freeLeftTimeTxt = ObjCreate.createTextField("0", 270, 2, 200, 20, this, GlobalStyle.textFormat6);
            this._nameTxt = ObjCreate.createTextField("", 65, 25, 80, 20, this, GlobalStyle.textFormat6);
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._valueTxt = ObjCreate.createTextField(Language.getStringByParam(26207, 0), 228, -71, 200, 20, this, new GTextFormat(FontUtil.songtiName, 12, 65280));
            this._valueTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._nameTxt.visible = false;
            return;
        }

        public updateLeftTimeAndModifyCount(arg1: int, arg2: int): void
        {
            this._diceMoneyModifyCount = arg1;
            this._cost = WuXingConfig.instance.getChangeEnergyCost(this._diceMoneyModifyCount + 1);
            this._freeLeftTime = arg2;
            if (this._freeLeftTime > 0) 
            {
                this._retryBtn.label = Language.getString(26209);
                this._retryBtn.toolTipData = Language.getStringByParam(26215, this._freeLeftTime);
                this.showTxtEffect(true, this._retryBtn, 0);
                this._labelTxt.text = Language.getString(26212);
                this._freeLeftTimeTxt.text = this._freeLeftTime.toString();
            }
            else 
            {
                this._retryBtn.label = Language.getString(26214);
                this._retryBtn.toolTipData = Language.getStringByParam(26213, this._cost);
                this.showTxtEffect(false, this._retryBtn);
                this._labelTxt.text = Language.getString(26225);
                this._freeLeftTimeTxt.text = this._cost.toString();
            }
            return;
        }

        /* internal  */showTxtEffect(arg1: Boolean, arg2: Object /* flash.display.DisplayObject */, arg3: int=3): void
        {
            var loc1=null;
            if (arg1) 
            {
                EffectManager.glowFilterReg(arg2, WuxingDiceResultView.filterArr, 0.8, 5, 2, arg3);
            }
            else 
            {
                EffectManager.glowFilterUnReg(arg2);
            }
            return;
        }

        /* internal */ static /* const */filterArr: Array<any>=[new Object /* flash.filters.GlowFilter */(268435302, 1, 6, 6, 4)];

        /* internal */ /* var */_titleTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_mainTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_mainTipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_valueTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_getBtn: GButton;

        /* internal */ /* var */_retryBtn: GButton;

        /* internal */ /* var */_isMax: Boolean;

        /* internal */ /* var */_freeLeftTime: int;

        /* internal */ /* var */_cost: int;

        /* internal */ /* var */_mainAttrName: string;

        /* internal */ /* var */_mainAttr: int;

        /* internal */ /* var */_diceMoneyModifyCount: int;

        /* internal */ /* var */_addEnergy: int;

        /* internal */ /* var */_preAddEnergy: int;

        /* internal */ /* var */_sameNum: int;

        /* internal */ /* var */_preSameNum: int;

        /* internal */ /* var */_dices: Array<any>;

        /* internal */ /* var */_wordItemArray: Array<any>;

        /* internal */ /* var */_isLoaded: Boolean;

        /* internal */ /* var */_freeLeftTimeTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_labelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_wxDice: SWuXingDice;
    }
